package com.woniu.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.core.exception.product.ProductExceptionCode;
import com.woniu.core.exception.product.TCourseTypeException;
import com.woniu.core.util.PageInfo;
import com.woniu.product.dao.TCourseTypeMapper;
import com.woniu.product.dto.TCourseTypeQueryDto;
import com.woniu.product.model.TCourseType;
import com.woniu.product.param.ReuseTCourseTypeChangeParam;
import com.woniu.product.param.TCourseTypeInsertParam;
import com.woniu.product.param.TCourseTypeQueryParam;
import com.woniu.product.service.TCourseTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@Service
public class TCourseTypeServiceImpl extends ServiceImpl<TCourseTypeMapper, TCourseType> implements TCourseTypeService {

    @Resource
    private TCourseTypeMapper tCourseTypeMapper;
    @Override
    public List<TCourseType> deleteByIds(Set<Integer> ids) throws Exception {
        List<TCourseType> tCourseTypes = tCourseTypeMapper.selectBatchIds(ids);
        if (tCourseTypes==null ||tCourseTypes.size()<=0 || tCourseTypes.isEmpty() ){
            throw new TCourseTypeException("课程类别不存在", ProductExceptionCode.TCOURSETYPE_EXCEPTION_CODE.getCode());
        }
        if (tCourseTypes.size()!=ids.size()){
            throw new TCourseTypeException("ids参数异常",ProductExceptionCode.TCOURSETYPE_EXCEPTION_CODE.getCode());
        }
        tCourseTypeMapper.deleteBatchIds(ids);

        return tCourseTypes;

    }

    @Override
    public TCourseType changeById(ReuseTCourseTypeChangeParam param) throws Exception {
        TCourseType type = tCourseTypeMapper.selectById(param.getCourseTypeId());
        if (type==null) {
            throw new TCourseTypeException("课程类别不存在",ProductExceptionCode.TCOURSETYPE_EXCEPTION_CODE.getCode());
        }
        if (param.getTypeName() != null && !param.getTypeName().isEmpty()){
            type.setTypeName(param.getTypeName());
        }
        if (param.getTypeState()!=null){
            if (param.getTypeState()==type.getTypeState()){
                throw new TCourseTypeException("课程类别状态重复，无需更改",ProductExceptionCode.TCOURSETYPE_EXCEPTION_CODE.getCode());
            }
            type.setTypeState(param.getTypeState());
        }
        tCourseTypeMapper.updateById(type);
        return type;
    }

    @Override
    public TCourseType queryTypeById(Integer courseTypeId) throws Exception {
        TCourseType type = tCourseTypeMapper.selectById(courseTypeId);
        if (type==null) {
            throw new TCourseTypeException("课程类别不存在",ProductExceptionCode.TCOURSETYPE_EXCEPTION_CODE.getCode());
        }
        return type;
    }

    @Override
    public PageInfo<TCourseTypeQueryDto> queryAll(TCourseTypeQueryParam param) throws Exception {
        Page<TCourseType> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 10 : param.getPageSize());
        QueryWrapper<TCourseType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_state",1);
        if (ObjectUtil.isNotEmpty(param.getTypeName())){
            queryWrapper.like("type_name",param.getTypeName());
        }
        if (ObjectUtil.isNotEmpty(param.getCreateAdmin())){
            queryWrapper.eq("create_admin",param.getCreateAdmin());
        }
        tCourseTypeMapper.selectPage(page,queryWrapper);
        System.out.println(page.getPages());
        System.out.println(page.getTotal());
        List<TCourseType> records = page.getRecords();
        if (records==null ||records.size()<=0 || records.isEmpty() ){
            throw new TCourseTypeException("查询的课程类别不存在",ProductExceptionCode.TCOURSETYPE_EXCEPTION_CODE.getCode());
        }
        List<TCourseTypeQueryDto> dtos = BeanUtil.copyToList(records, TCourseTypeQueryDto.class);
        PageInfo info = BeanUtil.toBean(page, PageInfo.class);
        info.setList(dtos);
        info.setPages(page.getPages());
        return info;
    }

    @Override
    public TCourseType insert(TCourseTypeInsertParam param) throws Exception {
        //带client,typeName 要做唯一！
        TCourseType type = tCourseTypeMapper.selectOne(new QueryWrapper<TCourseType>().eq("type_name", param.getTypeName()));
        if (type!=null){
            throw new TCourseTypeException("课程类别名称重复，请重新输入",ProductExceptionCode.TCOURSETYPE_EXCEPTION_CODE.getCode());
        }
        TCourseType type1 = BeanUtil.toBean(param, TCourseType.class);
        type1.setTypeState(1);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String DateTime = now.format(formatter);
        type1.setCreateDate(DateTime);
        this.save(type1);
        return type1;
    }
}
